package com.kuaiditu.assistant.ui.dialog;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.kuaiditu.assistant.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Eenie on 2017/9/1.
 * Email: eeniework@gmail.com
 * Des:
 */

@Route(path = TaskInfoDialog.PATH)
public class TaskInfoDialog extends DialogFragment implements View.OnClickListener {
    public static final String PATH = "/dialog/task_info";


    private View parentView;

    private CharSequence hintText;

    TextView mTvInfo;

    @BindView(R.id.btn_enter)
    Button mBtnEnter;

    @BindView(R.id.img_logo)
    ImageView mImgLogo;

    @BindView(R.id.tv_title)
    TextView mTvTitle;

    @BindView(R.id.content_container)
    FrameLayout mContentContainer;

    private int textGravity = Gravity.LEFT;

    String btnHint;

    private View mContentView;
    LayoutInflater inflater;

    public void setOnTakeTaskClickListener(String btnText, onTakeTaskClickListener onTakeTaskClickListener) {
        this.btnHint = btnText;
        mOnTakeTaskClickListener = onTakeTaskClickListener;
    }

    private onTakeTaskClickListener mOnTakeTaskClickListener;


    public void setMessage(CharSequence msg) {
        this.hintText = msg;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.inflater = inflater;
        parentView = inflater.inflate(R.layout.dialog_task_info, container);
        ButterKnife.bind(this, parentView);
        return parentView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        mBtnEnter.setOnClickListener(this);

        if (getContentView() == null) {
            mContentView = inflater.inflate(R.layout.dialog_default_msg_layout, null);
            mTvInfo = (TextView) mContentView.findViewById(R.id.tv_task_info);
            mTvInfo.setText(hintText);
            mTvInfo.setGravity(textGravity);
        }

        mContentContainer.addView(mContentView);

        if (!TextUtils.isEmpty(btnHint)) {
            mBtnEnter.setText(btnHint);
            mBtnEnter.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        dismiss();
        if (mOnTakeTaskClickListener != null) {
            mOnTakeTaskClickListener.onClick();
        }
    }


    public interface onTakeTaskClickListener {
        void onClick();
    }


    public void setCustomView(View v) {
        mContentView = v;
    }


    private View getContentView() {
        return mContentView;
    }


    public void setTextGravity(int textGravity) {
        this.textGravity = textGravity;
    }
}
